package com.borrow.conteoller;


import com.borrow.service.BorrowService;
import com.borrow.service.BorrowServieFeign;
import com.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class BorrowController {

    @Autowired
    BorrowService borrowService;
    @Autowired
    BorrowServieFeign borrowServieFeign;


    @RequestMapping("/borrowBooktest")
    public int borrowBookTest(BorrowEntity entity) {
        return borrowService.addBorrow(entity);
    }


    @RequestMapping("/borrowBook")
    public ResultMap borrowBook(BorrowEntity entity) {
        //添加借书一个记录
        // 1，更新图书表的库存总数-1
        ResultMap resultMap = borrowServieFeign.lendBookFeign(entity.getBid());
        //  2，添加借书一个记录
        int i = borrowService.addBorrow(entity);
        if (i > 0) {
            resultMap.setCode(200);
            resultMap.setMessage("借书成功");
        } else {
            resultMap.setCode(-1);
            resultMap.setMessage("借书失败");
        }
        return resultMap;
    }

    @RequestMapping("/getBooks")
    public UIResult<List<Book>> getBookByFegin(BookVo vo) {
        UIResult<List<Book>> book = borrowServieFeign.getBook(vo);
        return book;
    }


    @RequestMapping("/borrowBookList")
    public UIResult<List<BorrowBookVo>> borrowBookList(BorrowBookVo vo) {
        int count = borrowService.borrowBookCount(vo);
        List<BorrowBookVo> list = borrowService.borrowBook(vo);
        UIResult uiResult = new UIResult();
        uiResult.setCount(count);
        uiResult.setData(list);
        return uiResult;

    }

}
